//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package servlet.property;

import dao.Func;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import utils.JDBCUtils;

@WebServlet({"/DoUptateProperty"})
public class DoUptateProperty extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public DoUptateProperty() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        String pname = "";
        String price = "0";
        String ptotal = "0";
        String jsonString = Func.readJSONString(request);
        JSONObject obj = JSONObject.fromObject(jsonString);
        pname = obj.get("_pname").toString();
        price = obj.get("_price").toString();
        if (obj.get("_ptotal") != null) {
            ptotal = obj.get("_ptotal").toString();
            if (ptotal.equals("0")) {
                ptotal = price;
            }
        } else {
            ptotal = price;
        }

        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null && !session.getAttribute("username").toString().equals("")) {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;

            try {
                connection = JDBCUtils.getConnection();
                String sqltemp = "select count(*) totalCount from asset where pname='" + pname + "'";
                Statement statement = connection.createStatement();
                int rowCount = 0;
                ResultSet rset = statement.executeQuery(sqltemp);
                if (rset.next()) {
                    rowCount = rset.getInt("totalCount");
                }

                JSONObject jsonmain = new JSONObject();
                String sql;
                int result;
                if (rowCount > 0) {
                    sql = "update asset set ptotal=?,price=? where pname=?";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, ptotal);
                    preparedStatement.setString(2, price);
                    preparedStatement.setString(3, pname);
                    result = preparedStatement.executeUpdate();
                    if (result != 0) {
                        response.setStatus(200);
                        jsonmain.put("code", "200");
                        jsonmain.put("msg", "updated");
                    } else {
                        response.setStatus(500);
                        jsonmain.put("code", "500");
                        jsonmain.put("msg", "Error");
                    }
                } else {
                    sql = "insert into asset values(?,?,?)";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, pname);
                    preparedStatement.setString(2, ptotal);
                    preparedStatement.setString(3, price);
                    result = preparedStatement.executeUpdate();
                    if (result != 0) {
                        response.setStatus(200);
                        jsonmain.put("code", "200");
                        jsonmain.put("msg", "added");
                    } else {
                        response.setStatus(500);
                        jsonmain.put("code", "500");
                        jsonmain.put("msg", "Error");
                    }
                }

                out.println(jsonmain);
            } catch (ClassNotFoundException var24) {
                ClassNotFoundException e = var24;
                e.printStackTrace();
            } catch (SQLException var25) {
                SQLException e = var25;
                e.printStackTrace();
            } finally {
                JDBCUtils.release((ResultSet)resultSet, preparedStatement, connection);
            }

        } else {
            response.setStatus(403);
            out.println("403 Forbidden");
        }
    }
}
